class TFReplicator extends TFPowerups;

state Activated
{
	function BeginState()
	{
		initialstate='activated';
        if(instigator==none) instigator=pawn(owner);
        Owner.PlaySound(ActivateSound,SLOT_Interface,3);
        bActive = true;
        settimer(2, true);
	}

    function Timer()
    {
        local float amount, amount2;
        local float ammo1, maxammo1, ammo2, maxammo2;
        local int count, save;
        local inventory inv;
        local tfweapon w;
        local bool bplaysound;


	    for( Inv=instigator.Inventory; Inv!=None && count<1000; Inv=Inv.Inventory )
	    {
		    Count++;
		    if(tfweapon(inv)!=none && tfweapon(inv).bDoAmmoRegen)
		    {
		        w=tfweapon(inv);

		        ammo1=w.AmmoAmount(0);
		        maxammo1=w.MaxAmmo(0);
		        ammo2=w.AmmoAmount(1);
		        maxammo2=w.MaxAmmo(1);

                if((ammo1<maxammo1 || (maxammo2>0 && ammo2<maxammo2)) && !w.GetFireMode(0).bisfiring && !w.GetFireMode(1).bisfiring)
                    bplaysound=true;

                if(bplaysound)
                {
		            if(maxammo1>0)
		            {
                    amount=w.default.AmmoRegenAmount[0]/2;  //See TFArmory for details on this code

                    save=int(amount);
		            amount-=save;
                    if(frand()<amount) amount=1;
                    amount+=save;
                    }

		            if(maxammo2>0)
		            {
                    amount2=w.default.AmmoRegenAmount[1]/2;

                    save=int(amount2);
                    amount2-=save;
                    if(frand()<amount2) amount2=1;
                    amount2+=save;
                    }

		            if(amount+amount2>=1)
                    {
                        if(maxammo1>0) w.addammo(amount,0);
                        if(maxammo2>0) w.addammo(amount2,1);
                    }
                }
            }
        }
        if(bplaysound) Owner.PlaySound(sound'TFMedia.TFRepliAmmo',SLOT_None,0.5);

        if(instigator==none || instigator.Health<=0) gotostate('');
    }

	function EndState()
	{
        bActive = false;
	}

	function Activate()
	{

	}
}

defaultproperties
{
     bAutoActivate=True
     bActivatable=True
     ActivateSound=Sound'TFMedia.Equipment.TFAmmoRepStart'
     bReplicateInstigator=True
     SkillCost=200
}
